import { handlers } from "./handlers";
import { isObject } from "./utils";

const proxyMap = new WeakMap();

// 要求obj是一个对象，否则无法做响应式
export function reactive(obj: any): any {
  if (!isObject(obj)) { 
    return obj
  }
  // 如果obj已经被代理过了，直接返回代理对象
  if(proxyMap.has(obj)) {
    return proxyMap.get(obj);
  }

  const proxy = new Proxy(obj, handlers)
  proxyMap.set(obj, proxy)

  return proxy
}